package com.itheima.core.service.impl;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.core.dao.UserDao;
import com.itheima.core.po.RegisterFormBean;
import com.itheima.core.po.User;
import com.itheima.core.service.UserService;
/**
 * 用户Service接口实现类
 */
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
	// 注入UserDao
	@Autowired
	private UserDao userDao;
	// 通过账号和密码查询用户
	@Override
	public User findUser(String usercode, String password) {
		User user = this.userDao.findUser(usercode, password);
		return user;
	}
	
    /**
     * 查询用户是否存在
     */
	@Override
	public boolean findUserIsExist(String user_code) {
		return userDao.findUserIsExist(user_code)==1;
	}
    
    /**
     * 插入用户
     */
	@Override
	public boolean register(RegisterFormBean registerFormBean) {
		HashMap<String,String> msg=new HashMap<>();
        User user = new User();
		boolean flag=true;
		user.setUser_code(registerFormBean.getUser_code());
		user.setUser_name(registerFormBean.getName());
		user.setUser_password(registerFormBean.getPassword());
		if (userDao.findUserIsExist(user.getUser_code())==1){
			msg.put("user_code","用户名已存在！");
			flag=false;
		}
		if (registerFormBean.getUser_code()==null||registerFormBean.getUser_code().trim().equals("")){
			msg.put("user_code","请输入账号！");
			flag=false;
		}else if (registerFormBean.getUser_code().length()>16 || registerFormBean.getUser_code().length()<2){
			msg.put("user_code","账号格式为2~16个字符！");
			flag=false;
		}
		if (registerFormBean.getPassword()==null||registerFormBean.getPassword().trim().equals("")){
			msg.put("password","请输入密码！");
			flag=false;
		} else if (registerFormBean.getPassword().length()>16 || registerFormBean.getPassword().length()<6){
			msg.put("password","密码格式不正确！");
			flag=false;
		}
		if (registerFormBean.getPassword2()==null||registerFormBean.getPassword2().trim().equals("")){
			msg.put("password2","请再次输入密码！");
			flag=false;
		} else if (!registerFormBean.getPassword().equals(registerFormBean.getPassword2())){
			msg.put("password2","两次密码不一致！");
			flag=false;
		}
		if (registerFormBean.getName()==null||registerFormBean.getName().trim().equals("")){
			msg.put("name","请输入姓名！");
			flag=false;
		}
		registerFormBean.setMsg(msg);
		if (flag){
			userDao.insertUser(user);
		}
		return flag;
	}
    
    /**
     * 删除用户
     */
	@Override
	public boolean deleteUser(User user) {
		return userDao.deleteUser(user)==1;
	}
    
    /**
     * 修改用户
     */
	@Override
	public boolean updateUser(User user) {
		return userDao.updateUser(user)==1;
	}
    
    /**
     * 查询所有用户
     */
    @Override
    public List<User> selectAllUser() {
        return userDao.selectAllUser();
    }
    
    /**
     * 批量删除
     */
    @Override
    public Integer deletePatch(List<String> user_code) {
        return userDao.deletePatch(user_code);
    }
    
}
